<template>
  <div style="padding: 40px;">
    <el-button type="danger" @click="logout">退出</el-button>
    <h2>系统主界面</h2>
    <p>欢迎 {{ userInfo?.uname }}！</p>
  </div>
</template>

<script setup lang="ts">
const config = useRuntimeConfig();
const token = useCookie('token');
const userInfo = ref<any>(null);

if (!token.value) {
  navigateTo('/login');
} else {
  const { data } = await useFetch('/userinfo', {
    baseURL: config.public.apiBase,
    headers: { Authorization: `Bearer ${token.value}` }
  });
  userInfo.value = data.value?.data;
}

const logout = () => {
  token.value = null;
  navigateTo('/login');
};
</script>
